package cn.fotoit.product.system.controller;

import cn.fotoit.product.system.entity.User;
import cn.fotoit.product.system.service.UserService;
import cn.fotoit.product.util.LogInfo;
import cn.fotoit.product.util.PasswordUtil;
import cn.fotoit.product.util.ReturnBean;
import io.swagger.annotations.Api;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(value = "用户接口", tags = "系统用户相关接口，用户添加，修改，删除，重置密码")
@RestController
@CrossOrigin
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserService userService;


    @PostMapping("add")
    @LogInfo(info = "用户添加", saveResponseData = true)
    @RequiresPermissions("system_user_add")
    public ReturnBean add(
            @RequestParam String nickName,
            @RequestParam String loginName,
            @RequestParam String loginPassword,
            @RequestParam Integer roleId,
            @RequestParam(required = false, defaultValue = "false") Boolean isLock) {
        User user = new User();
        user.setNickName(nickName);
        user.setLoginName(loginName);
        user.setLoginPassword(loginPassword);
        user.setRoleId(roleId);
        user.setIsLock(isLock);
        userService.add(user);
        return ReturnBean.success("添加成功");
    }

    @PostMapping("updateNotNull")
    @LogInfo(info = "用户修改", saveRequestData = true, saveResponseData = true)
    @RequiresPermissions("system_user_update")
    public ReturnBean updateNotNull(
            @RequestParam Integer id,
            String nickName,
            String loginName,
            Integer roleId,
            Boolean isLock) {
        User user = new User();
        user.setId(id);
        user.setNickName(nickName);
        user.setLoginName(loginName);
        user.setRoleId(roleId);
        user.setIsLock(isLock);
        userService.updateNotNull(user);
        return ReturnBean.success("修改成功");
    }

    @LogInfo(info = "根据用户id重置用户密码", saveRequestData = true)
    @PostMapping("updatePasswordById")
    @RequiresPermissions("system_user_resetPwd")
    public ReturnBean resetPwd(
            @RequestParam Integer id) {
        userService.updatePasswordById(id, "123456");
        return ReturnBean.success("修改成功,密码成功重置为：123456");
    }


    @PostMapping("deleteById")
    @LogInfo(info = "根据id删除用户", saveRequestData = true, saveResponseData = true)
    @RequiresPermissions("system_user_delete")
    public ReturnBean deleteById(@RequestParam Integer id) {
        userService.deleteById(id);
        return ReturnBean.success("删除成功");
    }

    @PostMapping("findAll")
    @LogInfo(info = "根据id查询用户", saveRequestData = true, saveResponseData = true)
    @RequiresPermissions("system_user_list")
    public ReturnBean findAll(
            @RequestParam(required = false, defaultValue = "1") Integer page,
            @RequestParam(required = false, defaultValue = "15") Integer size) {
        return ReturnBean.success("查询成功", userService.findAll(page, size));
    }

    @PostMapping("findById")
    @LogInfo(info = "根据id查询用户", saveRequestData = true, saveResponseData = true)
    @RequiresPermissions("system_user_list")
    public ReturnBean findById(
            @RequestParam Integer id) {
        return ReturnBean.success("查询成功", userService.findById(id));
    }


}
